import java.io.*;
import java.net.*;
import java.util.*;

class NoName 
{
  private int HOST_COUNT;

  private double matrixA [][];  private double matrixA1[][]; 
  private double matrixA2[][];  private double matrixA3[][];
  
  private double matrixB [][];  private double matrixC [][];

  private double matrixCZ[][];  private double matrixCZ1[][]; 
  private double matrixCZ2[][]; private double matrixCZ3[][];
  
  private double matrixZ[][];   private double matrixZ1[][]; 
  private double matrixZ2[][];  private double matrixZ3[][];
  private int bad_hosts=0;
  private int d1,d2,d3;
	  
NoName(double matrixB[][],double matrixC[][], double matrixZ[][], String [] adress, int [] ports) throws Exception
  { d1=d2=d3=1;
  matrixA = new double[matrixB.length][matrixZ[0].length];
  bad_hosts = HOST_COUNT = matrixZ[0].length;

 System.out.print("Transferring data to MyHosts.......\n");
             Socket [] s  = new Socket[HOST_COUNT];
 ObjectOutputStream []oos = new ObjectOutputStream[HOST_COUNT] ;
  ObjectInputStream []ois = new ObjectInputStream[HOST_COUNT];
 try{
 for(int i=0;i<HOST_COUNT;i++)
 {
     s[i] = new Socket(adress[i],ports[i]);
   oos[i] = new ObjectOutputStream(s[i].getOutputStream());
   ois[i] = new ObjectInputStream(s[i].getInputStream());
 }
 }catch(Exception e)
 {System.out.println("network exceptions: "+e);}

if(bad_hosts==1)
{	  
     matrixCZ=new double[matrixC.length][d1];
 try{  
  oos[0].writeObject(matrixC); oos[0].writeObject(matrixZ); oos[0].flush();
  System.out.println(" host1 ---->");
  matrixCZ=(double [][])ois[0].readObject();
  System.out.println(" host1<--");
  oos[0].writeObject(matrixB); oos[0].writeObject(matrixCZ); oos[0].flush(); 
  System.out.println(" host1 oooo>");
  matrixA=(double [][])ois[0].readObject();
  System.out.println(" host1 <oooo");
}catch(Exception e){System.out.println("Exception: "+e);}
}else
{ 
if(bad_hosts==2)
  { 
 matrixCZ1 = new double[matrixC.length][d1];
 matrixCZ2 = new double[matrixC.length][d2];
 matrixZ1  = new double[matrixZ.length][d1];
 matrixZ2  = new double[matrixZ.length][d2];
 matrixA1  = new double[matrixB.length][d1];
 matrixA2  = new double[matrixB.length][d2];
 for(int i=0;i<matrixZ.length;i++)
 {matrixZ1[i][0]=matrixZ[i][0]; matrixZ2[i][0]=matrixZ[i][1]; }
try{
 oos[0].writeObject(matrixC); oos[0].writeObject(matrixZ1); oos[0].flush();
System.out.println(" host1 ---->");
oos[1].writeObject(matrixC); oos[1].writeObject(matrixZ2); oos[1].flush();
System.out.println(" host2 ---->");
matrixCZ1=(double [][])ois[0].readObject();
System.out.println(" host1<--");
matrixCZ1=(double [][])ois[1].readObject();
System.out.println(" host2<--");
oos[0].writeObject(matrixB); oos[0].writeObject(matrixCZ1); oos[0].flush(); 
System.out.println(" host1 oooo>");
oos[1].writeObject(matrixB); oos[1].writeObject(matrixCZ2); oos[1].flush(); 
System.out.println(" host2 oooo>");
matrixA1=(double [][])ois[0].readObject();
System.out.println(" host1 <oooo");
matrixA2=(double [][])ois[1].readObject();
System.out.println(" host2 <oooo");
for(int i=0;i<matrixA.length;i++) 
 { matrixA[i][0]=matrixA1[i][0]; matrixA[i][1]=matrixA2[i][0]; }
}catch(Exception e){System.out.println("Exception: "+e);}
  }//  3 bad hosts
  else { 

 matrixCZ1 = new double[matrixC.length][d1];
 matrixCZ2 = new double[matrixC.length][d2];
 matrixCZ3 = new double[matrixC.length][d3];
 matrixZ1  = new double[matrixZ.length][d1];
 matrixZ2  = new double[matrixZ.length][d2];
 matrixZ3  = new double[matrixZ.length][d3];
 matrixA1  = new double[matrixB.length][d1];
 matrixA2  = new double[matrixB.length][d2];
 matrixA3  = new double[matrixB.length][d3];
 for(int i=0;i<matrixZ.length;i++)
 {matrixZ1[i][0]=matrixZ[i][0]; 
  matrixZ2[i][0]=matrixZ[i][1]; 
  matrixZ3[i][0]=matrixZ[i][2];}
       
oos[0].writeObject(matrixC); oos[0].writeObject(matrixZ1); oos[0].flush();
System.out.println(" host1 ---->");
oos[1].writeObject(matrixC); oos[1].writeObject(matrixZ2); oos[1].flush();
System.out.println(" host2 ---->");
oos[2].writeObject(matrixC); oos[2].writeObject(matrixZ3); oos[2].flush();
System.out.println(" host3 ---->");
matrixCZ1=(double [][])ois[0].readObject();
System.out.println(" host1<--");
matrixCZ2=(double [][])ois[1].readObject();
System.out.println(" host2<--");
matrixCZ3=(double [][])ois[2].readObject();
System.out.println(" host3<--");
oos[0].writeObject(matrixB); oos[0].writeObject(matrixCZ1); oos[0].flush(); 
System.out.println(" host1 oooo>");
oos[1].writeObject(matrixB); oos[1].writeObject(matrixCZ2); oos[1].flush(); 
System.out.println(" host2 oooo>");
oos[2].writeObject(matrixB); oos[2].writeObject(matrixCZ3); oos[2].flush();
System.out.println(" host3 oooo>");
matrixA1=(double [][])ois[0].readObject();
System.out.println(" host1 <oooo");
matrixA2=(double [][])ois[1].readObject();
System.out.println(" host2 <oooo");
matrixA3=(double [][])ois[2].readObject();
System.out.println(" host3 <oooo");
       for(int i=0;i<matrixA.length;i++) 
 { matrixA[i][0]=matrixA1[i][0]; matrixA[i][1]=matrixA2[i][0]; 
   matrixA[i][2]=matrixA3[i][0];}
  }

  FileWriter fw = new FileWriter("result.txt",false);
for(int i=0;i<matrixA.length;i++)
{  for(int j=0;j<matrixA[0].length;j++)
  fw.write(matrixA[i][j]+" ");
  fw.write("\n");
}
  fw.close();
  System.exit(0);
  }
}
}
